<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { createTableDataApi, deleteTableDataApi, updateTableDataApi, getTableDataApi } from "@/api/table"
import { type CreateOrUpdateTableRequestData, type GetTableData } from "@/api/table/types/table"
import { type FormInstance, type FormRules, ElMessage, ElMessageBox } from "element-plus"
import { Search, Refresh, CirclePlus, Delete, Edit, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { useRoute, useRouter } from "vue-router"

const dialogVisible2 = ref(false)
const s3 = ["订单编号", "产品工号", "产品名称", "部件名称", "机号", "图号", "件号", "库位", "数量", "打包人", '仓库',]
const tableData = ref([
  {
    email0: "DD00000001",
    email01: "2024-04-08 13:22:23",
    email02: "生产部",
    email03: "2车间",
    email05: '根',
    email06: '3',
    email07: '1',
    email1: "100200176",
    email2: "印度",
    email3: "前大梁",
    email4: "待打包",
    email5: "5#",
    email6: "GYDE-2-3",
    email8: "1/2",
    // email8: 'B',
    email9: "23",
    email10: "涂装1#",
    email11: "张三",
    email12: "15899992222",
    email13: "",
    email14: "出库",
    type: ""
  },
  {
    email0: "DD00000002",
    email1: "100200176",
    email01: "2024-04-08 13:22:23",
    email02: "生产部",
    email03: "2车间",
    email05: '根',
    email06: '3',
    email07: '1',
    email2: "印度",
    email3: "前大梁",
    email4: "打包中",
    email5: "5#",
    email6: "GYDE-2-3",
    email8: "1/2",
    // email8: 'B',
    email9: "23",
    email10: "涂装1#",
    email11: "张三",
    email12: "15899992222",
    email13: "",
    email14: "入库",
    type: "success"
  },
  {
    email0: "DD00000003",
    email1: "100200176",
    email2: "印度",
    email01: "2024-04-08 13:22:23",
    email02: "生产部",
    email03: "2车间",
    email05: '根',
    email06: '3',
    email07: '1',
    email3: "前大梁",
    email4: "打包完成",
    email5: "5#",
    email6: "GYDE-2-3",
    email8: "1/2",
    // email8: 'B',
    email9: "23",
    email10: "涂装1#",
    email11: "张三",
    email12: "15899992222",
    email13: "",
    email14: "出库",
    type: "success"
  }
])
defineOptions({
  // 命名当前组件
  name: "ElementPlus"
})

const router = useRouter()
function goto(path: string) {
  router.push({ path })
}

const activeName = ref("first0")
const s1 = ["领料单", "出库单", "配送单", "转运单", "退库单", "入库单", "计划单", "已完成"]
const s2 = [
  {
    label: "申请部门",
    value: "生产部"
  },
  {
    label: "车间",
    value: "1车间"
  },
  {
    label: "班组",
    value: "2班组"
  },
  {
    label: "申请人",
    value: "张三"
  },
  {
    label: "电话",
    value: "13688889999"
  }
]

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 增
const DEFAULT_FORM_DATA: CreateOrUpdateTableRequestData = {
  id: undefined,
  username: "",
  password: ""
}
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = ref<CreateOrUpdateTableRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))
const formRules: FormRules<CreateOrUpdateTableRequestData> = {
  username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    const api = formData.value.id === undefined ? createTableDataApi : updateTableDataApi
    api(formData.value)
      .then(() => {
        ElMessage.success("操作成功")
        dialogVisible.value = false
        getTableData()
      })
      .finally(() => {
        loading.value = false
      })
  })
}
const resetForm = () => {
  formRef.value?.clearValidate()
  formData.value = JSON.parse(JSON.stringify(DEFAULT_FORM_DATA))
}
//#endregion

//#region 删
const handleDelete = (row: GetTableData) => {
  ElMessageBox.confirm(`确认删除？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {})
}
//#endregion

//#region 改
const handleUpdate = (row: GetTableData) => {
  dialogVisible.value = true
  formData.value = JSON.parse(JSON.stringify(row))
}
//#endregion

//#region 查
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  username: "",
  phone: ""
})
const getTableData = () => {
  loading.value = true
  getTableDataApi({
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize,
    username: searchData.username || undefined,
    phone: searchData.phone || undefined
  })
    .then(({ data }) => {
      paginationData.total = data.total
      // tableData.value = data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}
//#endregion

/** 监听分页参数的变化 */
// watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never">
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column type="index" label="序" width="50" />
          <el-table-column prop="email0" label="订单编号" align="center" />
          <el-table-column prop="email14" label="订单类型" align="center" />
          <el-table-column prop="email11" label="打包人" align="center" />
          <el-table-column prop="email02" label="部门" align="center" />
          <el-table-column prop="email03" label="车间" align="center" />
          <el-table-column prop="email11" label="申请人" align="center" />
          <el-table-column prop="email12" label="联系电话" align="center" width="120" />
          <el-table-column prop="" label="状态" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.type ? 'success' : 'info'" effect="plain">{{ scope.row.email4 }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="email01" label="申请日期" align="center" />
          <el-table-column prop="email2" label="产品名称" align="center" />
          <el-table-column prop="email1" label="产品工号" align="center" width="100" />
          <el-table-column prop="email5" label="机号" align="center" />
          <el-table-column prop="email05" label="单位" align="center" />
          <el-table-column prop="email06" label="数量(铲)" align="center" />
          <el-table-column prop="email07" label="数量(件)" align="center" />
          <el-table-column prop="email3" label="部件名称" align="center" />
          <el-table-column prop="email10" label="收货地址" align="center" />
          <el-table-column prop="email13" label="备注" align="center" />
          <el-table-column label="操作" width="100" align="center">
            <!-- <template #default="scope"> -->
              <span style="color: #409eff; cursor: pointer" @click="goto('/labels/cangku')">详情</span>
              <!-- <div v-if="!scope.row.type" style="cursor: pointer;">
                <span class="px-2.5">修改</span><br>
                <span style="color: red;" @click="handleDelete(scope.row)">删除</span>
              </div> -->
            <!-- </template> -->
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增/修改 -->
    <el-dialog
      v-model="dialogVisible"
      :title="formData.id === undefined ? '新增用户' : '修改用户'"
      @closed="resetForm"
      width="30%"
    >
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="formData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="密码" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-dialog>
    <el-dialog v-model="dialogVisible2" width="30%">
      <div class="flex">
        <div class="pr-3">打包员</div>
        <el-input v-model="formData.username" style="width: 300px;" placeholder="请选择" />
      </div>
      <template #footer>
        <el-button @click="dialogVisible2 = false">取消</el-button>
        <el-button type="primary">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style>
.el-divider--horizontal {
  margin: 0 0 18px 0;
}
</style>
<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
